<?php

namespace app\common\model\hospital;

use think\Model;


class GeneralTime extends Model
{

    

    

    // 表名
    protected $name = 'appointment_times';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];



    public static function addlivejobs($jobs,$live_id,$hospital_id,$week_name,$week_days)
    {

        $hastime=self::where(['time_id'=>$live_id])->select();

        foreach($hastime as $k5=>$v5) {
            $qhas = 0;

            foreach ($jobs as $k => $v) {
                if(isset($v['time_id'])){
                    if ($v['time_id']==$v5['time_id']&&$v['endtime']==$v5['endtime']&&$v['startime']==$v5['startime']&&$v5['week_days']==$week_days) {
                        $qhas = 1;
                        $jobs[$k]['live_resume_id'] = $v5['id'];
                    }
                }


            }

            if ($qhas == 0) {

                self::destroy(['id' => $v5['id']]);

            }

        }

        foreach ($jobs as $k => $v) {


            if (isset($v['live_resume_id'])) {
                //判断当前是否存在对应时间
                $livejob = self::where(['id' => $v['live_resume_id']])->find();
                if ($livejob) {
                    $livejob->week_name=$week_name;
                    $livejob->week_days=$week_days;
                    $livejob->endtime=$v['endtime'];
                    $livejob->startime=$v['startime'];
                    $livejob->updatetime = time();
                    $livejob->save();
                }
            } else {
                $data = [
                    'endtime'=>$v['endtime'],
                    'startime'=>$v['startime'],
                    'week_name'=>$week_name,
                    'time_id'=>$live_id,
                    'week_days'=>$week_days,
                    'createtime' => time(),
                ];
                $res=self::insertGetid($data);

            }


        }



    }








    public function time()
    {
        return $this->belongsTo('app\common\model\appointment\Time', 'time_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
